/*
    SUSAN® - Sole of Unix Save ANything

   
      Copyright (C) 2011-2016 Skyatlas Co. LTD

   

   
*/

#ifndef BAREOS_LIB_MESSAGES_RESOURCE_H_
#define BAREOS_LIB_MESSAGES_RESOURCE_H_

#include "lib/bareos_resource.h"

struct MessageDestinationInfo;

class MessagesResource : public BareosResource {
 public:
  std::string mail_cmd_;                            /* Mail command */
  std::string operator_cmd_;                        /* Operator command */
  std::string timestamp_format_;                    /* Timestamp format */
  std::vector<MessageDestinationInfo*> dest_chain_; /* chain of destinations */
  std::vector<char> send_msg_types_;

 private:
  bool in_use_ = false;  /* Set when using to send a message */
  bool closing_ = false; /* Set when closing message resource */

 public:
  MessagesResource();
  virtual ~MessagesResource();
  MessagesResource(const MessagesResource& other) = delete;
  MessagesResource(const MessagesResource&& other) = delete;
  MessagesResource& operator=(const MessagesResource& rhs) = default;
  MessagesResource& operator=(MessagesResource&& rhs) = delete;

  void ClearInUse();
  void SetInUse();
  void SetClosing();
  bool GetClosing() const;
  void ClearClosing();
  bool IsClosing() const;
  void WaitNotInUse() const;
  void Lock() const;
  void Unlock() const;

  bool PrintConfig(OutputFormatterResource& send,
                   const ConfigurationParser& /* unused */,
                   bool hide_sensitive_data,
                   bool verbose = false) override;

  void DuplicateResourceTo(MessagesResource& other) const;
  void AddMessageDestination(MessageDestinationCode dest_code,
                             int msg_type,
                             const std::string& where,
                             const std::string& mail_cmd,
                             const std::string& timestamp_format);
  void RemoveMessageDestination(MessageDestinationCode dest_code,
                                int msg_type,
                                const std::string& where);


 private:
  std::vector<MessageDestinationInfo*> DuplicateDestChain() const;
  bool AddToExistingChain(MessageDestinationCode dest_code,
                          int msg_type,
                          const std::string& where);
  void AddToNewChain(MessageDestinationCode dest_code,
                     int msg_type,
                     const std::string& where,
                     const std::string& mail_cmd,
                     const std::string& timestamp_format);
  std::string GetMessageTypesAsSring(MessageDestinationInfo* d,
                                     bool verbose = false);
};

#endif /* BAREOS_LIB_MESSAGES_RESOURCE_H_ */
